package org.wheel.jpa.demo.service.impl;

import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.lang.Nullable;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wheel.base.service.impl.AbstractService;
import org.wheel.jpa.demo.domain.SysTest;
import org.wheel.jpa.demo.repositories.SysTestRepository;
import org.wheel.jpa.demo.service.SysTestService;

/**
 * @author klaus.wang
 */
@Service("sysTestService")
public class SysTestServiceImpl extends AbstractService<SysTestRepository, SysTest> implements SysTestService {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveEntity(String entityName, Map<String, Object> entity) {
		
		Session session = entityManager.unwrap(Session.class);
		
		session.saveOrUpdate(entityName, entity);
		
	}
	
	
	@Override
	public Map get(String entityName, String id) {
		
		Session session = entityManager.unwrap(Session.class);
		
		Object val = session.get(entityName, id);
		
		return (Map) val;
		
	}

	@Override
	public void test(SysTestRepository sysTestRepository) {
		sysTestRepository.findAll();
	}

	@Override
	public void quartz() {
		try {
			Thread.sleep(1000);
			dao.findAll(Sort.by("listNo"));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}


}
